package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.myBeans.R;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.*;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 23:45
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/jobapply")
@Api(description = "应聘控制",value = "jobapply")
public class JobApplyController extends BaseService {
    @Autowired
    private JobApplyService jobApplyService;
    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private RecruitInfoService recruitInfoService;
    @Autowired
    private PositionTypeService positionTypeService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private CvService cvService;
    /**
     * TateBrown 2018年5月26日
     *
     * @param jobApply
     * @return
     */
    @ApiOperation("根据id修改应聘表记录")
    @PostMapping("/editById")
    public R editById(@RequestBody JobApply jobApply){
        try {
            if (checkDeleteUpdateIsSuccess(jobApplyService.modifyOneById(jobApply))) {
                return R.ok("修改成功");
            } else {
                return R.error("修改失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }

    /**
     * TateBrown 2018年5月26日
     *
     * @param jobApply
     * @return
     */
    @ApiOperation("投递简历")
    @PostMapping("/addOne")
    public R addOne(@RequestBody JobApply jobApply){
        try {
            jobApplyService.addOne(jobApply);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param jobApply
     * @return
     */
    @ApiOperation("学生应聘模糊筛选")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody JobApply jobApply){
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (jobApply.getPageIndex() > 0) jobApply.setPageIndex(jobApply.getPageIndex() - 1);
            List<JobApply> jobApplyList = jobApplyService.findByCondition(jobApply);
            for (JobApply jobApply1 : jobApplyList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("jobApply", jobApply1);
                //查找用人单位名称信息
                if (jobApply1.getEmployerid() != null) {
                    OrgInfo orgInfo = orgInfoService.findByid(jobApply1.getEmployerid());
                    jsonObject.put("orgInfoFullName", orgInfo.getFullname());
                }
                //查找对应岗位
                if (jobApply1.getRecruitinfoid() != null){
                    RecruitInfo recruitInfo = recruitInfoService.findRecruitInfoById(jobApply1.getRecruitinfoid());
                    if (recruitInfo != null){
                        PositionType positionType = positionTypeService.findById(recruitInfo.getPositiontype());
                        jsonObject.put("positionTypeName", positionType.getTypename());
                    }
                }
                jsonObjectList.add(jsonObject);
            }
            return R.ok("查找成功").put("data",jsonObjectList).put("total", MyPageHelper.getTotal(jsonObjectList));
        } catch (Exception e) {
           return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param jobApply
     * @return
     */
    @ApiOperation("企业应聘模糊筛选")
    @PostMapping("/findByCondition2")
    public R findByCondition2(@RequestBody JobApply jobApply){
        List<JSONObject> jsonObjectList = new ArrayList<>();
        try {
            if (jobApply.getPageIndex() > 0) jobApply.setPageIndex(jobApply.getPageIndex() - 1);
            List<JobApply> jobApplyList = jobApplyService.findByCondition(jobApply);
            for (JobApply jobApply1 : jobApplyList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("jobApply", jobApply1);
                //获取学生详细信息
                if (jobApply1.getStid() != null){
                    StudentInfoDetail studentInfoDetail = studentInfoService.findDetailByUserId(jobApply1.getStid());
                    jsonObject.put("studentInfoDetail", studentInfoDetail);
                }
                //获取简历信息
                if (jobApply1.getCvid() != null){
                    Cv cv = cvService.findOneById(jobApply1.getCvid());
                    jsonObject.put("cv", cv);
                }
                //获取对应招聘信息
                if (jobApply1.getRecruitinfoid() != null){
                    RecruitInfo recruitInfo = recruitInfoService.findRecruitInfoById(jobApply1.getRecruitinfoid());
                    jsonObject.put("recruitInfo", recruitInfo);
                    if (recruitInfo != null){
                        jsonObject.put("positiontype", positionTypeService.findById(recruitInfo.getPositiontype()));
                    }
                }
                jsonObjectList.add(jsonObject);
            }

            return R.ok("查找成功").put("data",jsonObjectList).put("total",MyPageHelper.getTotal(jsonObjectList));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查找失败");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找单条应聘记录")
    @PostMapping("/findOneById/{id}")
    public R findOneById(@PathVariable("id") Integer id){
        try {
            return R.ok("查询成功").put("data",jobApplyService.findOneById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }
}
